from vnpy_ctastrategy import (
    CtaTemplate,
    StopOrder,
    TickData,
    BarData,
    TradeData,
    OrderData,
    BarGenerator,
    ArrayManager,
)

class run_example(CtaTemplate) :
    fast_window = 10
    slow_window = 20
    ###############################################
    fast_ma = 0.0
    slow_ma = 0.0
    ###############################################
    parameters = ["fast_window", "slow_window"]
    parameters_nm = ['fast_window', 'slow_window']
    variables = ["fast_ma", "slow_ma"]
    ###############################################
    def __init__(self, cta_engine, strategy_name, vt_symbol, setting) :
        super().__init__(cta_engine, strategy_name, vt_symbol, setting)
        ###############################################
        self.last_tick = None
        ###############################################
    def on_init(self) :
        self.write_log('策略初始化:symbol[{}]'.format(self.vt_symbol) )
        ###############################################
        self.bg = BarGenerator(self,
                               self.on_bar,
                               nightEndT = self.contract.nightEndT,)
        self.am = ArrayManager(self.slow_window+3)
        ###############################################
        self.load_bar(
                       days = 7,
                       callback = self.on_load_bar,
                       use_database = False,
                      )
        ###############################################
    def on_start(self) :
        self.write_log('策略启动:symbol[{}]'.format(self.vt_symbol) )
        self.put_event()

    def on_stop(self) :
        self.write_log('策略停止:symbol[{}]'.format(self.vt_symbol) )
        self.put_event()

    def on_tick(self, tick: TickData) :
        self.write_logfile('on_tick:symbol[{}],time[{}],exch[{}],'
                           'bidV[{}],bid[{}],ask[{}],askV[{}],lastP[{}]'.format(
                           tick.symbol,
                           tick.datetime,
                           tick.exchange,
                           tick.bid_volume_1,
                           tick.bid_price_1,
                           tick.ask_price_1,
                           tick.ask_volume_1,
                           tick.last_price,
                          )
        )
        ####################################################
        self.bg.update_tick(tick)
        ####################################################
        if self.last_tick and tick.last_price > self.last_tick.last_price :
            self.write_log('on_tick:symbol[{}],buy'.format(self.vt_symbol) )
            sprice = tick.ask_price_1
            if self.pos == 0 :
                self.buy(sprice, 1)
            elif self.pos < 0 :
                self.buy(sprice, 2)
        else :
            self.write_log('on_tick:symbol[{}],sell'.format(self.vt_symbol) )
            sprice = tick.bid_price_1
            if self.pos == 0 :
                self.sell(sprice, 1)
            elif self.pos > 0 :
                self.sell(sprice, 2)
        ####################################################
        self.last_tick = tick

    def on_load_bar(self, bar: BarData, isLastBar=False) :
        self.write_log('on_load_bar:symbol[{}]'.format(self.vt_symbol) )
        am = self.am
        am.update_bar(bar)
        for i in range(1, self.slow_window+2) :
            self.write_log('date[{}],open[{}],high[{}],low[{}],close[{}]'.format(
                           self.am.date_array[-i],
                           self.am.open_array[-i],
                           self.am.high_array[-i],
                           self.am.low_array[-i],
                           self.am.close_array[-i],
                           ))

    def on_bar(self, bar: BarData) :
        self.write_log('on_bar:symbol[{}]'.format(self.vt_symbol) )
        am = self.am
        am.update_bar(bar)
        for i in range(1, self.slow_window+2) :
            self.write_log('date[{}],open[{}],high[{}],low[{}],close[{}]'.format(
                           self.am.date_array[-i],
                           self.am.open_array[-i],
                           self.am.high_array[-i],
                           self.am.low_array[-i],
                           self.am.close_array[-i],
                           ))
        if not am.inited :
            return
        ####################################################
        self.fast_ma = am.sma(self.fast_window)
        self.slow_ma = am.sma(self.slow_window)
        ####################################################
        self.put_event()

    def on_order(self, order: OrderData) :
        pass

    def on_trade(self, trade: TradeData) :
        self.put_event()

